import dayjs from "dayjs";
import isoWeek from "dayjs/plugin/isoWeek";
import note from "./note";
import tags from "./tags";
dayjs.extend(isoWeek);
const recordList: RecordDay[] = [];
type RecordDay = {
  currentDate: string;
  currentWeek: string;
  output: number;
  input: number;
  dayOfWeek: number;
  dayOfMonth: number;
  dayOfYear: number;
  tag: TagType[];
  note: string;
};
const weekHash = ["天", "一", "二", "三", "四", "五", "六"];
function parseToRecordDay(record: RecordType): RecordDay {
  const cDate = dayjs(record.createdAt).format("M月DD日");
  const num = parseInt(dayjs(record.createdAt).format("d"));
  const dayOfWeek = dayjs(record.createdAt).isoWeek();
  const dayOfMonth = dayjs(record.createdAt).date();
  const dayOfYear = dayjs(record.createdAt).year();
  const cWeek = "星期" + weekHash[num];
  let output = 0;
  let input = 0;
  if (record.category === "0") {
    output = record.amount;
  } else {
    input = record.amount;
  }
  return {
    currentDate: cDate,
    currentWeek: cWeek,
    output,
    input,
    dayOfWeek,
    dayOfMonth,
    dayOfYear,
    tag: record?.tags || [],
    note: record.note,
  };
}
type recordMap = {
  currentDate: string;
  currentWeek: string;
  totalInput: number;
  totalOutput: number;
  dayOfWeek: number;
  dayOfMonth: number;
  dayOfYear: number;
  children: {
    output: number;
    input: number;
    tags: TagType[];
    note: string;
  }[];
};
type RecordHash = {
  [index: string]: RecordDay[];
};
function parseToRecordDayList(records: RecordType[]) {
  const recordHash: RecordHash = {};
  const recordMaps: recordMap[] = [];

  const recordDays = records.map((record) => {
    return parseToRecordDay(record);
  });
  recordDays.map((recordDay) => {
    if (recordHash[recordDay.currentDate]) {
      recordHash[recordDay.currentDate].push(recordDay);
    } else {
      recordHash[recordDay.currentDate] = [recordDay];
    }
    return recordDay;
  });
  let key: keyof RecordHash;
  for (key in recordHash) {
    let records = recordHash[key];
    let totalInput = 0;
    let totalOutput = 0;
    let currentDate = "";
    let currentWeek = "";
    let dayOfWeek = 0;
    let dayOfMonth = 0;
    let dayOfYear = 0;
    const children: {
      output: number;
      input: number;
      tags: TagType[];
      note: string;
    }[] = [];
    records.map((v, i) => {
      totalInput += v.input;
      totalOutput += v.output;
      currentDate = v.currentDate;
      currentWeek = v.currentWeek;
      dayOfYear = v.dayOfYear;
      dayOfMonth = v.dayOfMonth;
      dayOfWeek = v.dayOfWeek;
      children.push({
        input: v.input,
        output: v.output,
        tags: v.tag,
        note: v.note,
      });
      return v;
    });
    recordMaps.push({
      dayOfMonth,
      dayOfYear,
      dayOfWeek,
      currentDate,
      currentWeek,
      totalInput,
      totalOutput,
      children,
    });
  }
  return recordMaps.reverse();
}
type RecordWeek = {
  totalInput: number;
  totalOutput: number;
  children: {
    input: number;
    output: number;
    weekOfDay: string;
  }[];
};
/**
 * type RecordType = {
  id: string,
  tags: TagType[],
  category:string,
  createdAt: string,
  amount: number,
}

 * @param records 
 */
function parseToRecordWeek(records: RecordType[]): RecordWeek {
  //按iso算今年的第几周 2021年 47周
  let curWeek = dayjs().isoWeek();
  let totalInput = 0;
  let totalOutput = 0;
  //本周的星期
  let children: {
    input: number;
    output: number;
    weekOfDay: string; //星期几string数字
  }[] = [];
  let weeks: RecordType[] = [];
  records.map((record) => {
    const week = dayjs(record.createdAt).isoWeek();
    //一周的七天

    //是不是最近一周
    if (week === curWeek) {
      if (record.category === "0") {
        //支出
        totalOutput += record.amount;
      } else {
        //收入
        totalInput += record.amount;
      }
      weeks.push(record);
    }

    return record;
  });
  children = parseToRecordDayList(weeks).map((value) => {
    return {
      input: value.totalInput,
      output: value.totalOutput,
      weekOfDay: value.currentWeek,
    };
  });
  //计算这一周内的7天每天的总收支

  return {
    totalInput,
    totalOutput,
    children,
  };
}

//---------------------------月统计------------------------------------------
type MonthRecord = {
  totalInput: number;
  totalOnput: number;
  children: {
    dayOfMonth: number; //是本月的第几天
    input: number;
    output: number;
  };
};
function parseToRecordMonth(records: RecordType[]) {
  const curMonth = dayjs().month();
  let totalInput = 0;
  let totalOutput = 0;
  let children: {
    dayOfMonth: number; //是本月的第几天
    input: number;
    output: number;
  }[] = [];
  const months: RecordType[] = [];
  records.map((record) => {
    const month = dayjs(record.createdAt).month();
    if (month === curMonth) {
      //是当月的数据
      if (record.category === "0") {
        //支出
        totalOutput += record.amount;
      } else {
        totalInput += record.amount;
      }
      months.push(record);
    }
  });

  //-----------得到本月的所有数据,把每天的数据汇总
  children = parseToRecordDayList(months).map((item) => {
    return {
      input: item.totalInput,
      output: item.totalOutput,
      dayOfMonth: item.dayOfMonth,
    };
  });
  return {
    totalInput,
    totalOutput,
    children,
  };
}

export {
  recordList,
  parseToRecordDay,
  parseToRecordDayList,
  parseToRecordWeek,
  parseToRecordMonth
};
